package org.example.market_spring.controller;

import com.sun.net.httpserver.Authenticator;
import org.example.market_spring.entity.Admin;
import org.example.market_spring.result.Result;
import org.example.market_spring.service.AdminService;
import org.example.market_spring.utils.JwtUtil;
import org.example.market_spring.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public R login(@RequestBody Admin admin) {
        Admin data = adminService.queryByUsernameAndPassword(admin);

        //登录成功，生成并下发令牌
        if(data!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",data.getId());
            claims.put("username",data.getUsername());

            String jwt = JwtUtil.createJWT("jwb", 10, claims);
            return R.ok(jwt);
            //return R.ok("登录成功").put("data", data);

        }
        //登陆失败，返回错误信息
        return R.error("用户名或密码错误");

    }
}
